#!/bin/bash

#获取主机名
Host=$(hostname)

#获取内网IP地址，存放到变量里，用逗号隔开
#ip a | grep inet -w | awk '{print $2}' | sed "s/\/..*$//g"
IPList=$(ip a | grep inet -w | awk '{print $2}' | sed "s/\/..*$//g" | xargs | sed "s/ /,/g")

#获取COU使用率
CpuUsage=$(top -b -n1 |grep Cpu | awk '{print $2+$4}')

#获取内存使用率
#总内存
#空闲内存
MenTotal=$(top -b -n1 |grep "MiB Mem" | awk '{print $4}')
MenFree=$(top -b -n1 |grep "MiB Swap" | awk '{print $(NF-2)}')
MenUsage=$(echo "scale=4;(($MenTotal-$MenFree) * 100)" / $MenTotal| bc)

#获取系统平均负载
#查看硬盘的忙碌程度
#iostat -h -k 11 -x -h
LoadList=$(uptime | awk '{print $(NF-2), $(NF-1), $NF}')

#监控应用
#1、进程名称
AppName=nginx
#2、进程ID
AppProcID=$(top -b -n1 | grep nginx | awk '{print $1}' | xargs | sed 's/ /\,/g')
#3、进程CPU使用率
AppCpuUsage=


#JSON格式
printf "{
  \"hostname\": \"$Host\", #主机名
  \"load\": \"[$LoadList]\", #1、5、15分钟负载
  \"mem\": \"$MenUsage%\", #内存使用率
  \"cpu\": \"$CpuUsage%\", #cpu使用率
  \"ip\": \"[$IPList]\" #所有的内网IP地址
}
"

#!/bin/bash
topfile="/root/top.txt"
cpufile="/root/cpu.txt"
procfile="/root/proc.txt"
jsonfile="/root/json.txt"

#获取当时top的数据保存在对应的文件里
top -b -n2 |tail -n +7 | grep -A 1000000 "load" > $topfile
sed -n "1,7p" $topfile > $cpufile
sed -n "8,1000000p" $topfile > $procfile

#1、5、15平均负载
load1=`grep average $topfile | awk '{print $(NF-2)}'`
load5=`grep average $topfile | awk '{print $(NF-1)}'`
load15=`grep average $topfile | awk '{print $NF}'`

CpuUsage=`grep Cpu $topfile | awk '{print $2 + $4}'`

CpuWa=`grep Cpi $topfile | awl '{print %10}'`

MenUsage=`free -b | grep Mem | awk '{printf ("%.1f\n", 100-($7/$2*100))}'`

if [ "wa" == "$CpuWa" ];then
  CpuWa=`grep Cpi $topfile | awl '{print %9}'`
fi

MonProcFile="/root/monproc.txt"
#$1取进程多少行进程
if [ -n "$(echo $1 | grep "^[[:digit:]]*$")" ];then
  head -n $1 $procfile | sed "s/[ ][ ]*/#/g" | sed "s/^#//g" > $MonProcFile
else
  grestr="$(echo $1 |sed "s/ ,/\\\|/g")"
  grep "$grepstr" $procfile | sed "s/[ ][ ]*/#/g" | sed "s/^#//g" > $MonProcFile
fi

proclist=$(cat $MonProcFile)
for proc in $proclist;do
  ppid=`echo $proc | awk -F "#" '{print $1}'`
  psts=`echo $proc | awk -F "#" '{print $8}'`
  pcpu=`echo $proc | awk -F "#" '{print $9}'`
  pmem=`echo $proc | awk -F "#" '{print $10}'`
  name=`echo $proc | awk -F "#" '{print $12}'`
  echo "{"  >> $jsonfile
  echo "\"host\":    \"$5\"," >> $jsonfile
  echo "\"host_name\":    \"$(hostname)\"," >> $jsonfile
  echo "\"host_cpu\":    \"$cpu\"," >> $jsonfile
  echo "\"host_mem\":    \"$mem\"," >> $jsonfile
  echo "\"host_load\":    \"$load\"," >> $jsonfile
  echo "\"host_wa\":    \"$cwa\"," >> $jsonfile
  echo "\"proc_name\":    \"$name\"," >> $jsonfile
  echo "\"proc_pid\":    \"$ppid\"," >> $jsonfile
  echo "\"proc_cpu\":    \"$pcpu\"," >> $jsonfile
  echo "\"proc_mem\":    \"$pmem\"," >> $jsonfile
  echo "\"proc_sts\":    \"$psts\"," >> $jsonfile
  echo "\"provbe_ver\":    \"$psts\"," >> $jsonfile
  echo "\"gl2_source_collector\":    \"$psts\"," >> $jsonfile
  echo "\"message-type\":    \"$psts\"," >> $jsonfile
  echo "\"messagte\":    \"$psts\"," >> $jsonfile
  echo "}" >> $jsonfile
  timeout 1 /usr/local/bin/uniprobe-worker -p $2 $jsonfile
done


#修改主机名，格式：公有云/私有云-地点-什么作用-编号
